package com.printeverywhere.delegator;

import static com.printeverywhere.util.AdminConstants.CALLED_ADD_METHOD;
import static com.printeverywhere.util.AdminConstants.SESSION_CLOSED;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;

import com.paypal.sdk.exceptions.PayPalException;
import com.paypal.sdk.services.CallerServices;
import com.paypal.soap.api.AddressType;
import com.paypal.soap.api.BasicAmountType;
import com.paypal.soap.api.CountryCodeType;
import com.paypal.soap.api.CreditCardDetailsType;
import com.paypal.soap.api.CreditCardTypeType;
import com.paypal.soap.api.CurrencyCodeType;
import com.paypal.soap.api.DoDirectPaymentRequestDetailsType;
import com.paypal.soap.api.DoDirectPaymentRequestType;
import com.paypal.soap.api.DoDirectPaymentResponseType;
import com.paypal.soap.api.PayerInfoType;
import com.paypal.soap.api.PaymentActionCodeType;
import com.paypal.soap.api.PaymentDetailsType;
import com.paypal.soap.api.PersonNameType;
import com.printeverywhere.dto.Creditcard;
import com.printeverywhere.dto.Deposits;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.HibernateUtil;

public class DepositsServiceDelegator {
	private static Logger logger = Logger
			.getLogger(DepositsServiceDelegator.class);

	/**
	 * update the amount to deposited table.
	 *
	 * @param Deposits
	 *            the Depositss
	 *
	 * @throws HibernateException
	 *             the hibernate exception
	 */
	public static void update(Deposits deposits) throws HibernateException {
		logger.info(AdminConstants.CALLED_UPDATE_METHOD);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			session.update(deposits);
			transaction.commit();
			logger.info(AdminConstants.RECORD_UPDATED);
		} catch (HibernateException e) {
			logger.error(AdminConstants.ERROR_IN_UPDATING_THE_RECORD);
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}

	public static Deposits get(Date date)
			throws HibernateException {
		logger.info("Called get method".intern());
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		Deposits Deposits = new Deposits();
		try {
			Criteria criteria = session.createCriteria(Deposits.class)
					.add(Expression.eq("depositDate", date));
			List<Deposits> list = criteria.list();
			Deposits = (list.size() > 0) ? list.get(0)
					: null;
			transaction.commit();
		} catch (HibernateException e) {
			logger.info("Error in getting the object for given categoryId "
					.intern()
					+ e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
		return Deposits;
	}

	public static void add(Deposits deposits) throws HibernateException {
		logger.info(CALLED_ADD_METHOD);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			session.save(deposits);
			transaction.commit();
			logger.info(AdminConstants.RECORD_ADDED);
		} catch (HibernateException e) {
			logger.error(AdminConstants.ERROR_IN_ADDING_THE_RECORD);
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}

	/*public static void DoDirectPayment(CallerServices caller, Creditcard creditcard) throws PayPalException {
    	System.out.println("\n########## Do Direct Payment ##########\n");

    	DoDirectPaymentRequestType request = new DoDirectPaymentRequestType();
		DoDirectPaymentRequestDetailsType details = new DoDirectPaymentRequestDetailsType();

		CreditCardDetailsType creditCard = new CreditCardDetailsType();
		creditCard.setCreditCardNumber("4721930402892796");
		creditCard.setCreditCardType(CreditCardTypeType.Visa);

		creditCard.setCVV2("000");
		creditCard.setExpMonth(new Integer(11));
		creditCard.setExpYear(new Integer(2010));

		PayerInfoType cardOwner = new PayerInfoType();
		cardOwner.setPayerCountry(CountryCodeType.US);

		AddressType address = new AddressType();
		address.setPostalCode("95101");
		address.setStateOrProvince("CA");
		address.setStreet1("123 Main St");
		address.setCountryName("US");
		address.setCountry(CountryCodeType.US);
		address.setCityName("San Jose");
		cardOwner.setAddress(address);

		PersonNameType payerName = new PersonNameType();
		payerName.setFirstName("SDK");
		payerName.setLastName("Buyer");
		cardOwner.setPayerName(payerName);

		creditCard.setCardOwner(cardOwner);
		details.setCreditCard(creditCard);

		details.setIPAddress("12.36.5.78");
		details.setMerchantSessionId("456977");
		details.setPaymentAction(PaymentActionCodeType.Sale);

		PaymentDetailsType payment = new PaymentDetailsType();

		BasicAmountType orderTotal = new BasicAmountType();
		orderTotal.setCurrencyID(CurrencyCodeType.USD);
		orderTotal.set_value("20.00");
		payment.setOrderTotal(orderTotal);

		details.setPaymentDetails(payment);
		request.setDoDirectPaymentRequestDetails(details);

		DoDirectPaymentResponseType response = (DoDirectPaymentResponseType) caller.call("DoDirectPayment", request);

    	System.out.println("Operation Ack: " + response.getAck());
 	  	System.out.println("---------- Results ----------");
 	  	System.out.println("\nTransaction ID: " + response.getTransactionID());
 	  	System.out.println("CVV2: " + response.getCVV2Code());
 	  	System.out.println("AVS: " + response.getAVSCode());
 	  	System.out.println("Gross Amount: " + response.getAmount().getCurrencyID()
			+ " " + response.getAmount().get_value());
    }*/

}
